package doc2db.stdstreams;

import doc2db.UserProp;
import java.awt.Color;
import java.awt.Cursor;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 *
 * @author yura
 */
public class MyOut {

    private static JTextPane text = new JTextPane(new LimitedStyledDocument());
    private JScrollPane scroll = new JScrollPane(text);
    public static PrintStream stdout = System.out;
    public static PrintStream stderr = System.err;
    public PrintStream out;
    public PrintStream err;

    private void initVar() {
        text.setBackground(Color.white);
        text.setEditable(false);
        text.setVisible(true);
        text.setComponentPopupMenu(new MyOutPropMenu(stdout, text).getPopupMenu());
        text.setCursor(new Cursor(Cursor.TEXT_CURSOR));
        scroll.setPreferredSize(scroll.getMaximumSize());

        SimpleAttributeSet atr = new SimpleAttributeSet();
        StyleConstants.setFontFamily(atr, UserProp.textFont.getFamily());
        StyleConstants.setFontSize(atr, UserProp.textFont.getSize());

        try {
            SimpleAttributeSet atr1 = new SimpleAttributeSet(atr);
            StyleConstants.setForeground(atr1, Color.DARK_GRAY);
            out = new Out("out", text, atr1);
        } catch (FileNotFoundException ex) {
            out = stdout;
        }

        try {
            SimpleAttributeSet atr1 = new SimpleAttributeSet(atr);
            StyleConstants.setForeground(atr1, Color.RED);
            err = new Out("out", text, atr1);
        } catch (FileNotFoundException ex) {
            err = stderr;
        }
    }

    public MyOut() {
        initVar();
    }

    public JScrollPane getPane() {
        return scroll;
    }

    public static class LimitedStyledDocument extends DefaultStyledDocument {

        /** Field maxCharacters */
        int maxLines;

        public LimitedStyledDocument(int maxLines) {
            this.maxLines = maxLines;
        }

        public LimitedStyledDocument() {
            this(500);
        }

        @Override
        public void insertString(int offs, String str, AttributeSet attribute)
                throws BadLocationException {

            int occurs = occurs(getText(0, getLength()), "\n");
            boolean b = /*!EOL.equals(str) ||*/ occurs < maxLines - 1;
            if (!b) {
                String[] split = getText(0, getLength()).split("\r\n|\n\r|\n|\r");
                if (split != null & split.length > 0) {
                    String substr = split[0];
                    int count = 1;
                    if (substr.endsWith("\r\n") | substr.endsWith("\n\r")){
                        count ++;
                    }
                    int removed = substr.length() + count;
                    offs -= removed;
                    remove(0, removed);
                }
            }
            super.insertString(offs, str, attribute);
        }

        private int occurs(String str, String subStr) {
            int occurrences = 0;
            int fromIndex = 0;

            while (fromIndex > -1) {
                int sp = occurrences == 0 ? fromIndex : fromIndex + subStr.length();
                fromIndex = str.indexOf(subStr, sp);
                if (fromIndex > -1) {
                    occurrences++;
                }
            }

            return occurrences;
        }
    }
}
